import { ajax2 , ajax} from '/ajax';
exports.getWebsocket = function(callback) {
  let that = this;
  //连接webSocket
  my.connectSocket({
    url: "wss://online02.tiaze.com:8282",
    success: (res) => {
      //监听webSocket连接是否成功
      my.onSocketOpen(function(res) {
        console.log('WebSocket 连接已打开！');
        // 接收服务器信息
        my.onSocketMessage(function(res) {
          let severData = JSON.parse(res.data);
          //获取除心跳以外的数据
          if (severData.type != "ping") {
            //打印服务器获取的消息
            console.log('收到服务器内容：',severData)
            /*******************************与后台握手（让后台知道当前设备的存在）******************** */
            if (severData.type == 'login') {
              let params = {
                type: 'login',
                login_type: '2',
                client_id: severData.client_id,
                user_phone: 'QCF420190927117689' //that.data.deviceNumber
              }
              ajax2('/WorkPost', params, 'POST').then((res) => {
                console.log(res)
              })
            }
            /*******************************监听事件********************************************** */
            let data = severData.msg;
            console.log(data)
            if(data != undefined){
              console.log('uuuuuuu.......')
              //监听退出事件
              if (data.type == 'toQuit') {
                console.log('监听到退出事件'); console.log('监听到退出事件');
              }
              //监听预授权事件
              if (data.type == 'toFundauthFreeze') {
                console.log('监听到预授权事件');
                callback(data.out_order_no, data.amount)
                // console.log('启动收银台')
                // console.log( data.out_order_no)
                // console.log(data.amount)
                // console.log(my.getStorageSync({ key: 'token' }).data)
                // console.log(data.amount)
              }
            }
          }
        })
      });
      console.log('connect url: ', res)
    },
    fail: (res) => {
      console.log('connect fail：' + JSON.stringify(res))
    }
  })
}


exports.fundauthPay = function(out_order_no, amount) {
  let that = this;
  // this.setData({out_trade_no: createOrderData.data.out_trade_no});
  console.log('启动收银台')
  //启动收银台
  my.ix.startApp({
    appName: 'cashier',
    bizNo: out_order_no,
    totalAmount: amount,
    orderDetail: [{ name: '支付', content: '普通商品'}],
    showScanPayResult: true,
    success: async (r) => {
      console.log('刷脸预授权。。。。。。',r);
      let parameData = {
        token: this.data.token,
        out_trade_no: this.data.out_trade_no,
        code: r.barCode
      }
      console.log(parameData)
      //支付接口
      let payResultData = await ajax("/SmallAppSmileFundauth", parameData, 'POST');
      if(payResultData.data.status === 1) {
        console.log('支付成功了。。。')
      }
    }
  })
}